import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Layout from '@/views/Layout'
import User from '@/views/User'
import Home from '@/views/Home'
import Search from '@/views/Search'
import SearchResult from '@/views/Search/SearchResult'
import ArticleDetail from '@/views/ArticleDetail'
import UserEdit from '@/views/User/UserEdit'
import Chat from '@/views/Chat'
import { getToken } from '@/utils/token'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/layout',
    component: Layout,
    children: [
      {
        path: 'home',
        component: Home
      },
      {
        path: 'user',
        component: User
      }
    ]
  },
  {
    path: '/search',
    component: Search
  },
  {
    path: '/search_result/:kw',
    component: SearchResult
  },
  {
    path: '/detail',
    component: ArticleDetail
  },
  {
    path: '/user_edit',
    component: UserEdit
  },
  {
    path: '/char',
    component: Chat
  }


]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (getToken()?.length > 0 && to.path == '/login') {
    next('/layout/home')
  } else {
    next()
  }

})

export default router
